﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using NLog.Web;

namespace Vav.Api
{
    public class Program
    {
        //public static void Main(string[] args)
        //{
        //    Console.WriteLine(string.Join(" ", args));
        //    var isDevelopment = (Debugger.IsAttached || args.Contains("--development"));
        //    var builder = CreateWebHostBuilder(
        //        args.Where(arg => arg != "--console" && arg != "--development").ToArray(),
        //        isDevelopment ? "https://*:9188;http://*:9186" : "https://*:443;http://*:80");
        //    var host = builder.Build();
        //    host.Run();

        //}
        //public static string GetLocalIp()
        //{
        //    ///获取本地的IP地址
        //    return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(i => i.ToString().Contains("192.168")).ToString();
        //}
        //public static IWebHostBuilder CreateWebHostBuilder(string[] args, string urls)
        //{
        //    var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        //    if (env == null)
        //    {
        //        env = "";
        //    }
        //    else
        //    {
        //        env = "." + env;
        //    }
        //    return WebHost.CreateDefaultBuilder(args)
        //        .UseKestrel(options =>
        //        {
        //            var isDevelopment = (Debugger.IsAttached || args.Contains("--development"));
        //            var pfxFile = $"{AppContext.BaseDirectory}/chain{env}.pfx";
        //            var pwdFile = $"{AppContext.BaseDirectory}/password{env}.txt";
        //            Console.WriteLine($"pfx file: {pfxFile} {File.Exists(pfxFile)}");
        //            Console.WriteLine($"pfx password is: {pwdFile} {File.Exists(pwdFile)}");
        //            if (!isDevelopment && File.Exists(pfxFile))
        //            {
        //                var password = File.ReadAllText(pwdFile);
        //                Console.WriteLine($"pfx password is: {password}");
        //                options.ConfigureHttpsDefaults(https =>
        //                {
        //                    https.ServerCertificate = new X509Certificate2(pfxFile, password);
        //                });
        //            }
        //        })
        //        .ConfigureLogging(config =>
        //        {
        //            config.ClearProviders();
        //        })
        //        .UseStartup<Startup>()
        //        .UseUrls(urls);
        //}
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                 .ConfigureLogging(logger =>
                  {
                      logger.ClearProviders();
                  })
                .UseNLog()
                .UseStartup<Startup>();

    }
}
